<template>
  <div class="login-form">
    <form @submit.prevent>
      <div class="form-group">
        <label for="username">用户名：</label>
        <input
          type="text"
          id="username"
          placeholder="请输入用户名"
          v-model="form.username"
        />
      </div>
      <div class="form-group">
        <label for="password">密码：</label>
        <input
          type="password"
          id="password"
          placeholder="请输入密码"
          v-model="form.password"
        />
      </div>
      <button type="button" class="submit-btn" @click="login">登录</button>
    </form>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
import { fetchLogin } from '@/apis/admin/login';
import { ref } from 'vue';

const router = useRouter();
const form = ref({
  username: '',
  password: ''
});

async function login() {
  const params = form.value;
  try {
    const res = await fetchLogin(params);
    console.log('登录成功:', res);
    router.push('/admin');
  } catch (error) {
    console.error('登录失败:', error);
  }
}
</script>

<style scoped>
.login-form h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #555;
}

.form-group input {
  width: 100%;
  padding: 0.6rem;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.submit-btn {
  width: 100%;
  padding: 0.7rem;
  background-color: #667eea;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  margin-left: 15px;
  margin-top: 20px;
}

.submit-btn:hover {
  background-color: #5a4b9a;
}
</style>
